<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
  <title>parse_batter_pbp_data (GamedayParser)</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
</head>
<body class="standalone-code">
  <pre><span class="ruby-comment cmt"># File lib/gameday_parser.rb, line 242</span>
        <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">parse_batter_pbp_data</span>(<span class="ruby-identifier">doc</span>, <span class="ruby-identifier">game</span>)
                <span class="ruby-identifier">doc</span>.<span class="ruby-identifier">root</span>.<span class="ruby-identifier">each_element</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">atbat</span><span class="ruby-operator">|</span>
                        <span class="ruby-identifier">batter</span> = <span class="ruby-constant">Player</span>.<span class="ruby-identifier">find</span>(<span class="ruby-identifier">:first</span>, <span class="ruby-identifier">:conditions=</span><span class="ruby-operator">&gt;</span><span class="ruby-value str">'gameday_id='</span><span class="ruby-operator">+</span><span class="ruby-identifier">atbat</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'batter'</span>])
                        <span class="ruby-identifier">pitcher</span> = <span class="ruby-constant">Player</span>.<span class="ruby-identifier">find</span>(<span class="ruby-identifier">:first</span>, <span class="ruby-identifier">:conditions=</span><span class="ruby-operator">&gt;</span><span class="ruby-value str">'gameday_id='</span><span class="ruby-operator">+</span><span class="ruby-identifier">atbat</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'pitcher'</span>])

                        <span class="ruby-identifier">existing_ab</span> = <span class="ruby-constant">AtBat</span>.<span class="ruby-identifier">find</span>(<span class="ruby-identifier">:first</span>, <span class="ruby-identifier">:conditions=</span><span class="ruby-operator">&gt;</span><span class="ruby-node">&quot;game_id=#{game.id} AND inning=#{atbat.attributes['inning']} AND number=#{atbat.attributes['num']}&quot;</span>)
                        <span class="ruby-keyword kw">if</span> <span class="ruby-operator">!</span><span class="ruby-identifier">existing_ab</span>
                                <span class="ruby-identifier">at_bat_obj</span> = <span class="ruby-constant">AtBat</span>.<span class="ruby-identifier">new</span>
                                <span class="ruby-identifier">at_bat_obj</span>.<span class="ruby-identifier">inning</span> = <span class="ruby-identifier">atbat</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'inning'</span>]
                                <span class="ruby-identifier">at_bat_obj</span>.<span class="ruby-identifier">number</span> = <span class="ruby-identifier">atbat</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'num'</span>]
                                <span class="ruby-identifier">at_bat_obj</span>.<span class="ruby-identifier">ball</span> = <span class="ruby-identifier">atbat</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'b'</span>]
                                <span class="ruby-identifier">at_bat_obj</span>.<span class="ruby-identifier">strike</span> = <span class="ruby-identifier">atbat</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'s'</span>]
                                <span class="ruby-identifier">at_bat_obj</span>.<span class="ruby-identifier">out</span> = <span class="ruby-identifier">atbat</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'o'</span>]
                                <span class="ruby-identifier">at_bat_obj</span>.<span class="ruby-identifier">batter_gd_id</span> = <span class="ruby-identifier">atbat</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'batter'</span>]
                                <span class="ruby-identifier">at_bat_obj</span>.<span class="ruby-identifier">pitcher_gd_id</span> = <span class="ruby-identifier">atbat</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'pitcher'</span>]
                                <span class="ruby-identifier">at_bat_obj</span>.<span class="ruby-identifier">stand</span> = <span class="ruby-identifier">atbat</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'stand'</span>]
                                <span class="ruby-identifier">at_bat_obj</span>.<span class="ruby-identifier">description</span> = <span class="ruby-identifier">atbat</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'des'</span>]
                                <span class="ruby-identifier">at_bat_obj</span>.<span class="ruby-identifier">event</span> = <span class="ruby-identifier">atbat</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'event'</span>]
                                <span class="ruby-identifier">at_bat_obj</span>.<span class="ruby-identifier">brief_event</span> = <span class="ruby-identifier">atbat</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'brief_event'</span>]
                                <span class="ruby-identifier">at_bat_obj</span>.<span class="ruby-identifier">batter_id</span> = <span class="ruby-identifier">batter</span>.<span class="ruby-identifier">id</span>
                                <span class="ruby-identifier">at_bat_obj</span>.<span class="ruby-identifier">pitcher_id</span> = <span class="ruby-identifier">pitcher</span>.<span class="ruby-identifier">id</span>
                                <span class="ruby-identifier">at_bat_obj</span>.<span class="ruby-identifier">game_id</span> = <span class="ruby-identifier">game</span>.<span class="ruby-identifier">id</span>
                                <span class="ruby-identifier">at_bat_obj</span>.<span class="ruby-identifier">save</span>
                                
                                <span class="ruby-identifier">atbat</span>.<span class="ruby-identifier">each_element</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">pitch</span><span class="ruby-operator">|</span>
                                        <span class="ruby-identifier">pitch_obj</span> = <span class="ruby-constant">Pitch</span>.<span class="ruby-identifier">new</span>
                                        <span class="ruby-identifier">pitch_obj</span>.<span class="ruby-identifier">at_bat_id</span> = <span class="ruby-identifier">at_bat_obj</span>.<span class="ruby-identifier">id</span>
                                        <span class="ruby-identifier">pitch_obj</span>.<span class="ruby-identifier">description</span> = <span class="ruby-identifier">pitch</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'des'</span>]    
                                        <span class="ruby-identifier">pitch_obj</span>.<span class="ruby-identifier">pitch_type</span> = <span class="ruby-identifier">pitch</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'type'</span>]
                                        <span class="ruby-identifier">pitch_obj</span>.<span class="ruby-identifier">gd_pitch_id</span> = <span class="ruby-identifier">pitch</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'id'</span>]
                                        <span class="ruby-identifier">pitch_obj</span>.<span class="ruby-identifier">x</span> = <span class="ruby-identifier">pitch</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'x'</span>]
                                        <span class="ruby-identifier">pitch_obj</span>.<span class="ruby-identifier">y</span> = <span class="ruby-identifier">pitch</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'y'</span>]
                                        <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">pitch</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'start_speed'</span>]
                                                <span class="ruby-identifier">pitch_obj</span>.<span class="ruby-identifier">start_speed</span> = <span class="ruby-identifier">pitch</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'start_speed'</span>]
                                                <span class="ruby-identifier">pitch_obj</span>.<span class="ruby-identifier">end_speed</span> = <span class="ruby-identifier">pitch</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'end_speed'</span>]
                                                <span class="ruby-identifier">pitch_obj</span>.<span class="ruby-identifier">sz_top</span> = <span class="ruby-identifier">pitch</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'sz_top'</span>]
                                                <span class="ruby-identifier">pitch_obj</span>.<span class="ruby-identifier">sz_bot</span> = <span class="ruby-identifier">pitch</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'sz_bot'</span>]
                                                <span class="ruby-identifier">pitch_obj</span>.<span class="ruby-identifier">pfx_x</span> = <span class="ruby-identifier">pitch</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'pfx_x'</span>]
                                                <span class="ruby-identifier">pitch_obj</span>.<span class="ruby-identifier">pfx_z</span> = <span class="ruby-identifier">pitch</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'pfx_z'</span>]
                                                <span class="ruby-identifier">pitch_obj</span>.<span class="ruby-identifier">px</span> = <span class="ruby-identifier">pitch</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'px'</span>]
                                                <span class="ruby-identifier">pitch_obj</span>.<span class="ruby-identifier">pz</span> = <span class="ruby-identifier">pitch</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'pz'</span>]
                                                <span class="ruby-identifier">pitch_obj</span>.<span class="ruby-identifier">x0</span> = <span class="ruby-identifier">pitch</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'x0'</span>]
                                                <span class="ruby-identifier">pitch_obj</span>.<span class="ruby-identifier">y0</span> = <span class="ruby-identifier">pitch</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'y0'</span>]
                                                <span class="ruby-identifier">pitch_obj</span>.<span class="ruby-identifier">z0</span> = <span class="ruby-identifier">pitch</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'z0'</span>]
                                                <span class="ruby-identifier">pitch_obj</span>.<span class="ruby-identifier">vx0</span> = <span class="ruby-identifier">pitch</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'vx0'</span>]
                                                <span class="ruby-identifier">pitch_obj</span>.<span class="ruby-identifier">vy0</span> = <span class="ruby-identifier">pitch</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'vy0'</span>]
                                                <span class="ruby-identifier">pitch_obj</span>.<span class="ruby-identifier">vz0</span> = <span class="ruby-identifier">pitch</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'vz0'</span>]
                                                <span class="ruby-identifier">pitch_obj</span>.<span class="ruby-identifier">ax</span> = <span class="ruby-identifier">pitch</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'ax'</span>]
                                                <span class="ruby-identifier">pitch_obj</span>.<span class="ruby-identifier">ay</span> = <span class="ruby-identifier">pitch</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'ay'</span>]
                                                <span class="ruby-identifier">pitch_obj</span>.<span class="ruby-identifier">az</span> = <span class="ruby-identifier">pitch</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'az'</span>]
                                                <span class="ruby-identifier">pitch_obj</span>.<span class="ruby-identifier">break_y</span> = <span class="ruby-identifier">pitch</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'break_y'</span>]
                                                <span class="ruby-identifier">pitch_obj</span>.<span class="ruby-identifier">break_angle</span> = <span class="ruby-identifier">pitch</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'break_angle'</span>]
                                                <span class="ruby-identifier">pitch_obj</span>.<span class="ruby-identifier">break_length</span> = <span class="ruby-identifier">pitch</span>.<span class="ruby-identifier">attributes</span>[<span class="ruby-value str">'break_length'</span>]
                                                <span class="ruby-identifier">pitch_obj</span>.<span class="ruby-identifier">at_bat_id</span> = <span class="ruby-identifier">at_bat_obj</span>.<span class="ruby-identifier">id</span>
                                        <span class="ruby-keyword kw">end</span>
                                        <span class="ruby-identifier">pitch_obj</span>.<span class="ruby-identifier">save</span>
                                <span class="ruby-keyword kw">end</span>
                        <span class="ruby-keyword kw">else</span>
                                <span class="ruby-comment cmt">#puts &quot;At bat already exists...&quot;    </span>
                        <span class="ruby-keyword kw">end</span>
                <span class="ruby-keyword kw">end</span>   
        <span class="ruby-keyword kw">end</span></pre>
</body>
</html>